package com.guanghua.brick.html.tag;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guanghua.brick.html.CommonReplace;
import com.guanghua.brick.html.IControl;
import com.guanghua.brick.html.IReplace;

public class ReplaceTag extends BodyTagSupport {
	
	private static Log logger = LogFactory.getLog(ReplaceTag.class);
	private IReplace replace = new CommonReplace();
	
	public String getReplacement() {
		return replace.getReplacement();
	}

	public String getTarget() {
		return replace.getTarget();
	}

	public boolean isAll() {
		return replace.isAll();
	}

	public boolean isRegex() {
		return replace.isRegex();
	}

	public void setAll(boolean all) {
		replace.setAll(all);
	}

	public void setRegex(boolean regex) {
		replace.setRegex(regex);
	}

	public void setReplacement(String replacement) {
		replace.setReplacement(replacement);
	}

	public void setTarget(String target) {
		replace.setTarget(target);
	}
	

	public int doEndTag() throws JspException {
		//找到父亲，并add depend
		Tag tag = this.getParent();
		if (tag instanceof IControl) {
			List<IReplace> list = ((IControl)tag).getReplaces();
			if (list != null) {
				list.add(replace);
			} else {
				list = new ArrayList<IReplace>();
				((IControl)tag).setReplaces(list);
				list.add(replace);
			}
		} else {
			logger.warn("replace parent must be Icontrol");
		}
				
		return BodyTagSupport.EVAL_PAGE;
	}
}
